# 从本地加载模型权重
# 你需要确保：
#
# 模型文件确实存在于该目录下；
# 目录中包含 configuration.json, pytorch_model.bin, tokenizer_config.json, vocab.txt 等必要文件；
# 如果你知道模型的具体 revision 版本号（如 v1.0.0），请加上；否则可以省略。


from modelscope import AutoModelForCausalLM, AutoTokenizer

model_name = "Qwen/Qwen3-0.6B"
local_model_path = "/home/aresen/1project/2python/hub/models/Qwen3-0.6B"

# 加载本地 tokenizer 和 model
tokenizer = AutoTokenizer.from_pretrained(local_model_path, revision="v1.0.0", local_files_only=True)
model = AutoModelForCausalLM.from_pretrained(
    local_model_path,
    revision="v1.0.0",
    local_files_only=True,
    torch_dtype="auto",
    device_map="auto"
                                             )


# prepare the model input
prompt = "Give me a short introduction to large language model."
messages = [
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,
    enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

# conduct text completion
generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=32768
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()

# parsing thinking content
try:
    # rindex finding 151668 (</think>)
    index = len(output_ids) - output_ids[::-1].index(151668)
except ValueError:
    index = 0

thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")

print("thinking content:", thinking_content)
print("content:", content)